import Loader from "../../../Common/Loader";
import gameConfig from "../../config/gameConfig";
import { mgrs, sers } from "../../system/OverallSystem ";
import gameListItem from "./gameListItem";
import PageBase from "./pageBase";
import zhutiListItem from "./zhutiListItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class zhutiList extends PageBase {

    public pageName: string = "主题选择";

    @property(cc.Node)
    item: cc.Node = null

    @property(cc.Node)
    content: cc.Node = null

    @property(cc.Node)
    scr1: cc.Node = null

    @property(cc.Node)
    scr2: cc.Node = null


    @property(cc.Node)
    scrContent1: cc.Node = null

    @property(cc.Node)
    scrContent2: cc.Node = null
    
    onLoad() {

    }

    start() {

        Loader.loadBundleRes('home', 'json/皮肤购买', (res) => {
            console.log(res.json)
            let data = res.json

            Loader.loadBundleRes('home', 'json/物品', (res2) => {
                let data2 = res2.json
                if(!this || !this.node)return
                this.initGame(data,data2)
            })

        })
        this._init();

    }

    tab(e,d){
        let node = e.currentTarget.parent
        for (const iterator of node.children) {
            iterator.getChildByName('select').active = false
        }
        e.currentTarget.getChildByName('select').active = true

        this.scr1.active = d==1? true: false
        this.scr2.active = d==2? true: false

    }

    listData = null

    initGame(data,data2) {
        if(this.scrContent1){
            this.scrContent1.removeAllChildren()
        }
        if(this.scrContent2){
            this.scrContent2.removeAllChildren()
        }


        let page1 = []
        let page2 = []

        for (const key in data) {
            if (Object.prototype.hasOwnProperty.call(data, key)) {
                const element = data[key];
                if(element['分页']==1){
                    page1.push(element)
                    // let item = cc.instantiate(this.item)
                    // item.parent = this.scrContent2
                    // item.x = 0
                    // item.getComponent(zhutiListItem).setData(i,2)
                }else{
                    page2.push(element)
                    // let item = cc.instantiate(this.item)
                    // item.parent = this.scrContent1
                    // item.x = 0
                    // item.getComponent(zhutiListItem).setData(i,1)
                }
            }
        }

        let zu1 = []
        for (const iterator of page1) {
            if(zu1.indexOf(iterator['获取方式'])<0){
                zu1.push(iterator['获取方式'])
            }
        }
        for (const iterator of zu1) {
            let rongqi = cc.instantiate(this.content)
            rongqi.parent = this.scrContent2
            rongqi.x = 0
            // rongqi.y = 0
            rongqi.getChildByName('biaoti').getComponent(cc.Label).string = iterator+'解锁'

            
            let n = 0
            for (const data of page1) {
                if(data['获取方式']==iterator){
                    let item = cc.instantiate(this.item)
                    item.parent = rongqi.getChildByName('scr').getChildByName('view').getChildByName('content')
                    item.y = 0
                    // item.x = 0
                    let wupin = data2[data['物品ID']]
                    item.getComponent(zhutiListItem).setDatas(wupin,data)
                    n++
                }
            }

            if(n>3){
                rongqi.getChildByName('scr').getComponent(cc.ScrollView).enabled = true
            }else{
                rongqi.getChildByName('scr').getComponent(cc.ScrollView).enabled = false
            }
        }

        let zu2 = []
        for (const iterator of page2) {
            if(zu2.indexOf(iterator['获取方式'])<0){
                zu2.push(iterator['获取方式'])
            }
        }
        for (const iterator of zu2) {
            let rongqi = cc.instantiate(this.content)
            rongqi.parent = this.scrContent1
            rongqi.x = 0
            // rongqi.y = 0
            rongqi.getChildByName('biaoti').getComponent(cc.Label).string = iterator+'解锁'

            let n = 0
            for (const data of page2) {
                if(data['获取方式']==iterator){
                    let item = cc.instantiate(this.item)
                    item.parent = rongqi.getChildByName('scr').getChildByName('view').getChildByName('content')
                    item.y = 0
                    // item.x = 0
                    let wupin = data2[data['物品ID']]
                    item.getComponent(zhutiListItem).setDatas(wupin,data)
                    n++
                }
            }

            if(n>3){
                rongqi.getChildByName('scr').getComponent(cc.ScrollView).enabled = true
            }else{
                rongqi.getChildByName('scr').getComponent(cc.ScrollView).enabled = false
            }
        }



        // for (let i = 0; i < 5; i++) {
        //     let item = cc.instantiate(this.item)
        //     item.parent = this.scrContent1
        //     item.x = 0
        //     item.getComponent(zhutiListItem).setData(i,1)
            
        // }

        // for (let i = 0; i < 3; i++) {
        //     let item = cc.instantiate(this.item)
        //     item.parent = this.scrContent2
        //     item.x = 0
        //     item.getComponent(zhutiListItem).setData(i,2)
        // }

        // for (const key in data) {
        //     if (Object.prototype.hasOwnProperty.call(data, key)) {
        //         const element = data[key];
        //         let item = cc.instantiate(this.item)
        //         item.parent = this.scrContent
        //         item.x = 0
        //         element.key = key
        //         item.getComponent(gameListItem).setData(element)
        //     }
        // }

    }

    protected _init(): void {

        this.open();
    }

    OnBtnConfirm() {
       
    }

    OnBtnClose() {
        mgrs().soundMgr.playBtnSound();
        this.onlyClose();
    }

    // update (dt) {}
}
